<!--
 * @Author: 米爱 1969407518@qq.com
 * @Date: 2025-06-17 09:46:13
 * @LastEditors: 米爱 1969407518@qq.com
 * @LastEditTime: 2025-08-11 16:26:30
 * @FilePath: \CraneOperator-xm\src\components\video.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="content">
    <div id="video-contianer">
      <div
        id="divPlugin"
        class="plugin"
        ref="video"
        style="
          width: 800px;
          height: 500px;
          margin-left: 50%;
          transform: translateX(-50%);
        "
      ></div>
    </div>
    <button class="id_login_btn" @click="clickHandle">身份证号登录</button>
  </div>
</template>

<script>
import $api from "../untils/api.js";
import SpeakTts from "speak-tts";
import { mapState } from "vuex";
export default {
  name: "videoCom",
  computed: {
    ...mapState(["szIP", "szPassword", "szDeviceIdentify"]),
  },
  props: {
    selectStatus: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      iPrototocol: 1, //http协议，1表示http协议，2表示https协议
      iPort: "80", //登录设备的端口号
      szUserName: "admin", //摄像头登录账号
      photoUrl: null,
      videoStream: null,
      content: "证件号码进入考试",
      timer: null,
      text: "",
      tts: new SpeakTts(),
      types: null,
      textStudentInfo: null,
    };
  },

  created() {
    this.init();
    this.types = localStorage.getItem("textType");
  },
  mounted() {
    // setTimeout(() => {
    //   this.login();
    // }, 1000);
    // setTimeout(() => {
    //   this.see();
    // }, 1500);
    this.tts.speak({
      text: "请考生面对摄像头进行人脸识别登录",
      queue: false,
      listeners: {
        onend: () => {},
      },
    });

    this.timer = setInterval(() => {
      this.takePicture();
    }, 3000);
  },
  beforeDestroy() {
    this.logOut();
    this.stopALLPlay();
  },
  destroyed() {
    clearInterval(this.timer);
    this.tts.cancel();
    // setTimeout(() => {
    this.breakDom();
    // }, 500);
  },
  methods: {
    takePicture() {
      WebVideoCtrl.I_CapturePicData().then((res) => {
        this.photoUrl = "data:image/jpeg;base64," + res;
        this.getPhoto(this.photoUrl);
      });
    },
    getPhoto(photoUrl) {
      $api.getPhoto({ base64: photoUrl, type: this.types }).then((res) => {
        if (res.data.code == "500" && res.data.msg !== "Index: 0, Size: 0") {
          this.$message.error(res.data.msg);
        }
        if (res.data.code == "200") {
          this.textStudentInfo = res.data.data;
          clearInterval(this.timer);
          this.$router.push({
            name: "studentInfo",
            params: {
              textStudentInfo: this.textStudentInfo,
              types: this.types || localStorage.getItem("textType"),
            },
          });
        }
      });
    },
    clickHandle() {
      this.$router.push({
        name: "idLogin",
        params: {
          types: this.types || localStorage.getItem("textType"),
          id: this.textStudentInfo,
        },
      });
    },

    init() {
      const self = this;
      WebVideoCtrl.I_InitPlugin({
        iWindowType: 1, //画面分割数，1就是1*1,2就是2*2
        bWndFull: false, //是否支持双击全屏
        cbInitPluginComplete: function () {
          //检查插件是否最新
          WebVideoCtrl.I_InsertOBJECTPlugin("divPlugin").then(
            () => {
              WebVideoCtrl.I_CheckPluginVersion().then((bFlag) => {
                if (bFlag) {
                  alert("检测到新的插件版本，请及时更新");
                }
              });
              // 插件初始化完成后执行登录和预览
              self.login();
            },
            () => {
              alert("插件初始化失败，请确认是否安装了插件");
            }
          );
        },
      });
    },
    login() {
      const self = this;
      WebVideoCtrl.I_Login(
        this.szIP,
        this.iPrototocol,
        this.iPort,
        this.szUserName,
        this.szPassword,
        {
          success: function (e) {
            console.log(e, "登录成功!");
            setTimeout(() => {
              self.see();
            }, 500);
          },
          error: function (e) {
            console.log(e, "登录失败!");
            WebVideoCtrl.I_GetLastError((fail) => {
              console.log(fail, "错误吗");
            });
          },
        }
      );
    },
    see() {
      WebVideoCtrl.I_StartRealPlay(this.szIP + "_" + this.iPort, {
        success: () => {
          console.log("预览成功！");
        },
        error: (err) => {
          console.error("预览失败，错误信息：", err);
        },
      });
    },
    stopPlay() {
      WebVideoCtrl.I_Stop({
        iWndIndex: 1, //想要停止的某个窗口
        success: () => {
          console.log("停止成功！");
        },
        error: (err) => {
          console.error("停止失败，错误信息：", err);
        },
      });
    },
    stopALLPlay() {
      WebVideoCtrl.I_StopAllPlay();
    },
    logOut() {
      WebVideoCtrl.I_Logout(this.szDeviceIdentify, {
        success: () => {
          console.log("登出成功");
          this.szDeviceIdentify = null; // 清空设备标识
        },
        error: (err) => {
          console.error("登出失败，错误信息：", err);
        },
      });
    },
    breakDom() {
      WebVideoCtrl.I_DestroyPlugin({
        success: () => {
          console.log("销毁成功");
        },
        error: (err) => {
          console.error("销毁失败，错误信息：", err);
        },
      });
    },
  },
};
</script>

<style scoped lang="scss">
.content {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}
.active-video-border {
  border: 2px salmon solid;
}
#video-contianer {
  .mask {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    cursor: pointer;
  }
}
.content {
  text-align: center;
}
.videos {
  width: 400px;
  height: 400px;
  border-radius: 50%;
  object-fit: cover;
  margin-bottom: 40px;
}
.btn {
  width: 300px;
  height: 60px;
  margin-left: 50%;
  font-size: 25px;
  transform: translate(-50%);
}
.btn > div {
  width: 250px;
  display: flex;
  align-items: center;
  margin: 0 auto;
}
.icon {
  width: 25px;
  height: 25px;
  margin-left: 20px;
}
.dirlog_content {
  width: 700px;
  height: 600px;
  position: fixed;
  left: 17%;
  top: 25%;
  text-align: center;
  background-color: #fff;
}
.dirlog_title {
  width: 100%;
  height: 60px;
  line-height: 60px;
  color: #fff;
  font-size: 30px;
  font-weight: bold;
  background-color: #1890ff;
}
.dirlog_tips {
  color: #ff6600;
  font-weight: bold;
  font-size: 23px;
  margin: 20px 0;
}
.dirlog_info {
  display: flex;
  justify-content: space-around;
  margin: 20px 0;
}
.dirlog_info > img {
  width: 250px;
  height: 300px;
  margin-top: 20px;
}
.dirlog_info > .info {
  color: #000000;
  font-size: 23px;
  text-align: left;
  margin: 25px 0;
}
.info > div {
  margin: 7px 0;
}
.dirlog_btn {
  display: flex;
  justify-content: space-between;
  width: 250px;
  height: 50px;
  margin: 0 auto;
}
.btn1,
.btn2 {
  border: none;
  width: 70px;
  height: 30px;
  border-radius: 5px;
}
.btn1 {
  color: #ffffff;
  background-color: #1890ff;
}
.btn2 {
  color: #ffffff;
  background-color: #ff6600;
}
.id_login_btn {
  width: 200px;
  height: 50px;
  margin-top: 3%;
  // transform: translateX(-50%);
  // margin-left: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  font-size: 25px;
}
</style>
